package com.hp

import com.hp.define.ActionMap
import org.apache.commons.io.IOUtils
import org.json.JSONArray
import org.json.JSONObject

public  class ConfigManager {
    private String rootPath
    private String originalPath
    private String contextPath
    private String configFileName = "config.json"
    private String parentPath = null
    private JSONObject jsonConfig = null
    private static  String SCRAWL_FILE_NAME = "scrawl"
    private static  String REMOTE_FILE_NAME = "remote"
    private String accessPath //访问路径

    private ConfigManager(String rootPath, String contextPath, String uri,String accessPath) throws FileNotFoundException, IOException {
        rootPath = rootPath.replace("\\", "/")
        this.rootPath = rootPath
        this.contextPath = contextPath
        if (contextPath.length() > 0) {
            this.originalPath = this.rootPath + uri.substring(contextPath.length())
        } else {
            this.originalPath = this.rootPath + uri
        }
        this.accessPath=accessPath

        this.initEnv()
    }

    public static ConfigManager getInstance(String rootPath, String contextPath, String uri,String accessPath) {
        try {
            return new ConfigManager(rootPath, contextPath, uri,accessPath)
        } catch (Exception var4) {
            return null
        }
    }

    public boolean valid() {
        return this.jsonConfig != null
    }

    public JSONObject getAllConfig() {
        return this.jsonConfig
    }

    public Map<String, Object> getConfig(int type) {
        Map<String, Object> conf = new HashMap()
        String savePath = null
        switch(type) {
            case ActionMap.UPLOAD_IMAGE:
                conf.put("isBase64", "false")
                conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"))
                conf.put("allowFiles", this.getArray("imageAllowFiles"))
                conf.put("fieldName", this.jsonConfig.getString("imageFieldName"))
                savePath = this.jsonConfig.getString("imagePathFormat")
                break
            case ActionMap.UPLOAD_SCRAWL:
                conf.put("filename", "scrawl")
                conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"))
                conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"))
                conf.put("isBase64", "true")
                savePath = this.jsonConfig.getString("scrawlPathFormat")
                break
            case ActionMap.UPLOAD_VIDEO:
                conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"))
                conf.put("allowFiles", this.getArray("videoAllowFiles"))
                conf.put("fieldName", this.jsonConfig.getString("videoFieldName"))
                savePath = this.jsonConfig.getString("videoPathFormat")
                break
            case ActionMap.UPLOAD_FILE:
                conf.put("isBase64", "false")
                conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"))
                conf.put("allowFiles", this.getArray("fileAllowFiles"))
                conf.put("fieldName", this.jsonConfig.getString("fileFieldName"))
                savePath = this.jsonConfig.getString("filePathFormat")
                break
            case ActionMap.CATCH_IMAGE:
                conf.put("filename", "remote")
                conf.put("filter", this.getArray("catcherLocalDomain"))
                conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"))
                conf.put("allowFiles", this.getArray("catcherAllowFiles"))
                conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]")
                savePath = this.jsonConfig.getString("catcherPathFormat")
                break
            case ActionMap.LIST_FILE:
                conf.put("allowFiles", this.getArray("fileManagerAllowFiles"))
                conf.put("dir", this.jsonConfig.getString("fileManagerListPath"))
                conf.put("count", this.jsonConfig.getInt("fileManagerListSize"))
                break
            case ActionMap.LIST_IMAGE:
                conf.put("allowFiles", this.getArray("imageManagerAllowFiles"))
                conf.put("dir", this.jsonConfig.getString("imageManagerListPath"))
                conf.put("count", this.jsonConfig.getInt("imageManagerListSize"))
        }

        conf.put("savePath", savePath)
        conf.put("rootPath", this.rootPath)
        conf.put("accessPath", this.accessPath)
        return conf
    }

    private void initEnv() throws FileNotFoundException, IOException {
        File file = new File(this.originalPath)
        if (!file.isAbsolute()) {
            file = new File(file.getAbsolutePath())
        }

        this.parentPath = file.getParent()
//        String configContent = this.readFile(this.getConfigPath())
        String configContent = this.filter(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("config.json")))

        try {
            JSONObject jsonConfig = new JSONObject(configContent)
            this.jsonConfig = jsonConfig
        } catch (Exception var4) {
            this.jsonConfig = null
        }

    }

    private String getConfigPath() {

        String path=this.class.getClassLoader().getResource( "config.json").getPath()
//        return this.parentPath + File.separator + "config.json"
        return  path
    }

    private String[] getArray(String key) {
        JSONArray jsonArray = this.jsonConfig.getJSONArray(key)
        String[] result = new String[jsonArray.length()]
        int i = 0

        for(int len = jsonArray.length(); i < len ;++i) {
            result[i] = jsonArray.getString(i)
        }

        return result
    }

    private String readFile(String path) throws IOException {
        StringBuilder builder = new StringBuilder()

        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8")
            BufferedReader bfReader = new BufferedReader(reader)
            String tmpContent = null

            while((tmpContent = bfReader.readLine()) != null) {
                builder.append(tmpContent)
            }

            bfReader.close()
        } catch (UnsupportedEncodingException var6) {
            
        }

        return this.filter(builder.toString())
    }

    private String filter(String input) {
        return input.replaceAll("/\\*[\\s\\S]*?\\*/", "")
    }
}
